home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / polyray / util / makemap.c < prev    next >
C/C++ Source or Header  |  1994-03-15  |  2KB  |  71 lines

  1. #include <stdio.h>
  2.  
  3. typedef float map_entry[5];
  4.  
  5. static void
  6. read_cmap(char *ifilename, char *mapname)
  7. {
  8.    FILE *ifile = fopen(ifilename, "r");
  9.    char buffer[512];
  10.    map_entry cmap[16];
  11.    int i, j, cnt;
  12.  
  13.    if (ifile == NULL) {
  14.       printf("Failed to open input file: '%s'\n", ifilename);
  15.       exit(1);
  16.       }
  17.  
  18.    fgets(buffer, 512, ifile);
  19.    i = sscanf(buffer, "%d", &cnt);
  20.    if (i != 1) {
  21.       printf("Unable to read map entries\n");
  22.       exit(1);
  23.       }
  24.  
  25.    i = 0;
  26.    while (i <= cnt && !feof(ifile)) {
  27.       fgets(buffer, 512, ifile);
  28.       j = sscanf(buffer, "%f %f %f %f %f",
  29.          &cmap[i][0], &cmap[i][1], &cmap[i][2], &cmap[i][3],
  30.          &cmap[i][4]);
  31.       if (i > 0 && i < cnt)
  32.      fgets(buffer, 512, ifile);
  33.       if (j != 5) {
  34.      printf("Bad # of entries at line %d of map file\n", i+1);
  35.      exit(1);
  36.      }
  37.       i++;
  38.       }
  39.  
  40.    fclose(ifile);
  41.  
  42.    /* Now write the Polyray color map */
  43.    printf("define %s color_map(\n", mapname);
  44.    for (j=0;j<i-1;j++) {
  45.       printf("   [%5.3f, %5.3f, <%5.3f, %5.3f, %5.3f>, %5.3f, <%5.3f, %5.3f, %5.3f>, %5.3f]",
  46.        cmap[j][0], cmap[j+1][0], 
  47.        cmap[j][1], cmap[j][2], cmap[j][3], cmap[j][4],
  48.        cmap[j+1][1], cmap[j+1][2], cmap[j+1][3], cmap[j+1][4]);
  49.       if (j<i-2)
  50.      printf("\n");
  51.       else
  52.      printf(")\n");
  53.       }
  54. }
  55.  
  56. int
  57. main(argc,argv)
  58.    int argc;
  59.    char *argv[];
  60. {
  61.    FILE *file;
  62.    char *infile;
  63.  
  64.    if (argc != 3) {
  65.       printf("Usage: makemap cmapfile mapname\n");
  66.       exit(1);
  67.       }
  68.    else
  69.       read_cmap(argv[1], argv[2]);
  70. }
  71.